package com.ggqq.day315;
//力扣 https://leetcode-cn.com/problems/volume-of-histogram-lcci/
public class water {
    public static void main(String[] args){
        int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
        water water = new water();
        System.out.println(water.trap(height));
    }
    public int trap(int[] height){
        if(height.length<3){//长度小于三·没办法储水
            return 0;
        }
        int left = 0;//左指针
        int right = height.length-1;//右指针
        int leftmax = height[left];
        int rightmax = height[right];
        int res = 0;//储水

        while(left<right){
            if(leftmax<rightmax){
                res += leftmax-height[left];
                left++;
                leftmax =Math.max(height[left],leftmax);
            }else{
                res += rightmax-height[right];
                right--;
                rightmax = Math.max(height[right],rightmax);
            }
        }

        return res;

    }
}
